package org.rooster.chromosomeevaluator.rules;

import org.jgap.IChromosome;
import org.rooster.chromosomeevaluator.common.ScheduleMetrix;
import org.rooster.genetic.GenericRule;

/**
 * Created by IntelliJ IDEA.
 * User: manorama
 * Date: 6/5/12
 * Time: 9:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class SameTaskForContinuousSlotsBonusRule implements GenericRule{

    private int weight = 1;

    public SameTaskForContinuousSlotsBonusRule() {
    }

    public SameTaskForContinuousSlotsBonusRule(int weight) {
        this.weight = weight;
    }

    /**
     * If the same employee is assigned to same section in two consecutive time slots
     * this method adds a bonus value for it
     * @param chromosome
     * @return the total bonus value
     */

    @Override
    public double fitnessValueForRule(IChromosome chromosome) {

//        int[][] assignments = new int[ResourceManager.TOTAL_NO_OF_EMPLOYEES][TaskManager.NO_OF_TIME_SLOTS];
//        int bonus = 0;
//        int penalty = 0;
//        int previousSlot = 0;
//        int thisSlot = 0;
//        int nextSlot = 0;
//        int previousSlotStart = 0;
//        int previousSlotEnd = 0;
//        int thisSlotStart = 0;
//        int nextSlotStart = 0;
//        int thisSlotEnd = 0;
//        int nextSlotEnd = 0;
//        Gene[] genes = chromosome.getGenes();
//
//        for(int i=0; i<TaskManager.NO_OF_SECTIONS; i++) {
//            for (int j=0; j<TaskManager.NO_OF_TIME_SLOTS; j++) {
//                thisSlotStart = GeneIndex.getTimeSlotStart(i, j);
//                thisSlotEnd = GeneIndex.getTimeSlotEnd(i, j);
//                for(int k=thisSlotStart; k<= thisSlotEnd; k++) {
//                    assignments[(Integer)genes[k].getAllele()-1][j] = i+1;
//                }
//            }
//        }
//
//        for (int i=0; i<assignments.length; i++) {
//            for (int j=1; j<assignments[i].length-1; j++) {
//                if(assignments[i][j - 1] == assignments[i][j]) {
//                    bonus++;
//                    if(assignments[i][j] == assignments[i][j + 1]) {
//                        bonus++;
//                    }
//                } else if(!(assignments[i][j - 1] == assignments[i][j]) && !(assignments[i][j] == assignments[i][j + 1])) {
//                    penalty++;
//                }
//            }
//        }
        
        int[][] tempassigment = ScheduleMetrix.getScheduleMetrixInstance().getScheduleMetrixArray();
        int tempP=1;
        int tempB=1;
        for (int i=0; i<tempassigment.length; i++) {
            for (int j=1; j<tempassigment[i].length-1; j++) {
                if(Math.abs(tempassigment[i][j - 1]) == Math.abs(tempassigment[i][j])) {
                    tempB++;
                    if(Math.abs(tempassigment[i][j]) == Math.abs(tempassigment[i][j + 1])) {
                        tempB++;
                    }
                } else if(!(Math.abs(tempassigment[i][j - 1]) == Math.abs(tempassigment[i][j])) && !(Math.abs(tempassigment[i][j]) == Math.abs(tempassigment[i][j + 1]))) {
                    tempP++;
                }
            }
        }

//        for (int i = 0; i< TaskManager.NO_OF_SECTIONS; i++) {
//            for(int j=1; j< TaskManager.NO_OF_TIME_SLOTS-1; j++) {
//                previousSlot = j-1;
//                thisSlot = j;
//                nextSlot = j+1;
//                previousSlotStart = GeneIndex.getTimeSlotStart(i, previousSlot);
//                previousSlotEnd = GeneIndex.getTimeSlotEnd(i, previousSlot);
//                thisSlotStart = GeneIndex.getTimeSlotStart(i, thisSlot);
//                nextSlotStart = GeneIndex.getTimeSlotStart(i, nextSlot);
//                thisSlotEnd = GeneIndex.getTimeSlotEnd(i, thisSlot);
//                nextSlotEnd = GeneIndex.getTimeSlotEnd(i, nextSlot);
//
//                for(int k=thisSlotStart; k<=thisSlotEnd; k++) {
//                    for(int l=nextSlotStart; l<=nextSlotEnd; l++) {
//                        for(int m=previousSlotStart; m<=previousSlotEnd; m++) {
//                            if(genes[l].getAllele().equals(genes[k].getAllele()) && genes[k].getAllele().equals(genes[m].getAllele())) {
//                                bonus++;
//                            }
//                        }
//                    }
//
//                }
//
//            }
//        }
//        System.out.println(penalty+":"+tempP+" "+bonus+":"+tempB);
        return 1.0* weight *tempB/tempP;
    }

    @Override
    public int getWeight() {
        return weight;
    }

    @Override
    public void setWeight(int value) {
        this.weight = value;
    }
}